//
//  LLLoginViewModel.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLLoginViewModel.h"
#import "LLHTTPSessionManager.h"

@interface LLLoginViewModel ()
@property(nonatomic, strong, readwrite) RACCommand *loginCommand;
@end

@implementation LLLoginViewModel

- (void)initialize {
    [super initialize];

    RACSignal *validLoginSignal = [[RACSignal
            combineLatest:@[RACObserve(self, username), RACObserve(self, password)]
                   reduce:^(NSString *username, NSString *password) {
                       return @(username.length > 0 && password.length > 0);
                   }]
            distinctUntilChanged];
    @weakify(self)
    self.loginCommand = [[RACCommand alloc] initWithEnabled:validLoginSignal signalBlock:^RACSignal *(id input) {
        @strongify(self)
        LLUser *user = [[LLUser alloc] init];
        user.loginType = EM_LoginTypeSMS;
        user.phone = self.username;
        user.checkCode = self.password;
        return [[[LLHTTPSessionManager shareManager] loginWithUser:user]
                doNext:^(LLUser *user) {
                    NSLog(@"user:%@", user);
                }];
    }];
}

@end
